package org.tp8.sampleapp.loader.service.impl;

import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.stereotype.Service;
import org.tp8.sampleapp.loader.exc.SampleLoaderException;
import org.tp8.sampleapp.loader.service.intf.RESTService;

@Service
public class RESTServiceImpl implements RESTService {

	@Override
	public <T extends Object> T post(String url, String inContentType,
			String outContentType, String username, String password,
			Object input, Class<T> clazz) throws SampleLoaderException {

		WebClient client = WebClient.create(url);
		// client.path("bookstore/books");
		client.type(inContentType).accept(outContentType);

		String usernamePass = username + ":" + password;
		String authorizationHeader = "Basic "
				+ org.apache.cxf.common.util.Base64Utility.encode(usernamePass
						.getBytes());
		client.header("Authorization", authorizationHeader);

		Response response = client.post(input);

		return response.readEntity(clazz);
	}

}
